﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.HVAC
{
	public partial class SelectElbowFittingForm : System.Windows.Forms.Form
	{
		public SelectElbowFittingForm()
		{
			this.InitializeComponent();
			//base.Icon = Resource.YArchitechIcon;
		}

		private void SelectElbowFittingForm_Load(object sender, EventArgs e)
		{
			HVACUserSetting hvacuserSetting = new HVACUserSetting();
			this.shapeNum = Convert.ToInt32(hvacuserSetting.GetDuctShapeStyle());
			this.roundConn = this.cdm.GetDuctConnStyle(hvacuserSetting.GetDuctConnRoundElbowStyle());
			this.rectangleConn = this.cdm.GetDuctConnStyle(hvacuserSetting.GetDuctConnElbowStyle());
			GroupBox gBox = null;
			if (this.shapeNum == 0)
			{
				this.tabControl1.SelectTab("tpRound");
				this.ductConn = this.roundConn;
				gBox = this.groupBox2;
			}
			if (this.shapeNum == 1)
			{
				this.tabControl1.SelectTab("tpRectangle");
				this.ductConn = this.rectangleConn;
				gBox = this.groupBox1;
			}
			Button buttonByConnStyle = this.cdm.GetButtonByConnStyle(this.ductConn, gBox);
			if (buttonByConnStyle != null)
			{
				this.cdm.SelectButton(buttonByConnStyle);
			}
		}

		private void btn_Click(object sender, EventArgs e)
		{
			Color backColor = this.groupBox1.BackColor;
			Button button = sender as Button;
			if (button == null)
			{
				return;
			}
			this.cdm = new ConnectDuctManage();
			if (this.tabControl1.SelectedTab.Name == "tpRectangle")
			{
				this.cdm.ChangeButtnBackColor(this.groupBox1, button);
				this.rectangleConn = this.cdm.GetConnectStyle(button);
				this.ductConn = this.rectangleConn;
			}
			if (this.tabControl1.SelectedTab.Name == "tpRound")
			{
				this.cdm.ChangeButtnBackColor(this.groupBox2, button);
				this.roundConn = this.cdm.GetConnectStyle(button);
				this.ductConn = this.roundConn;
			}
		}

		private void btnBack_MouseDown(object sender, MouseEventArgs e)
		{
			if (e.Clicks == 2)
			{
				base.Close();
			}
		}

		protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
		{
			if (keyData == Keys.Escape)
			{
				base.Close();
				return true;
			}
			return base.ProcessCmdKey(ref msg, keyData);
		}

		private void SelectElbowFittingForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			HVACUserSetting hvacuserSetting = new HVACUserSetting();
			hvacuserSetting.WriteDuctConnElbowStyle(this.rectangleConn.ToString());
			hvacuserSetting.WriteDuctConnRoundElbowStyle(this.roundConn.ToString());
			if (this.tabControl1.SelectedTab.Name == "tpRectangle")
			{
				hvacuserSetting.WriteDuctShapeStyle("1");
			}
			if (this.tabControl1.SelectedTab.Name == "tpRound")
			{
				hvacuserSetting.WriteDuctShapeStyle("0");
			}
		}

		private void SelectElbowFittingForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
		{
			Button button = null;
			TabControl tabControl = sender as TabControl;
			if (tabControl.SelectedTab.Name == "tpRound")
			{
				button = this.cdm.GetButtonByConnStyle(this.roundConn, this.groupBox2);
			}
			if (tabControl.SelectedTab.Name == "tpRectangle")
			{
				button = this.cdm.GetButtonByConnStyle(this.rectangleConn, this.groupBox1);
			}
			if (button != null)
			{
				this.cdm.SelectButton(button);
			}
		}

		private void tabControl1_Selected(object sender, TabControlEventArgs e)
		{
			Button button = null;
			if (e.TabPage.Name == "tpRound")
			{
				button = this.cdm.GetButtonByConnStyle(this.roundConn, this.groupBox2);
			}
			if (e.TabPage.Name == "tpRectangle")
			{
				button = this.cdm.GetButtonByConnStyle(this.rectangleConn, this.groupBox1);
			}
			if (button != null)
			{
				this.cdm.SelectButton(button);
			}
		}

		public ConnectDuctStyle ductConn = ConnectDuctStyle.Empty;

		private ConnectDuctManage cdm = new ConnectDuctManage();

		private ConnectDuctStyle rectangleConn = ConnectDuctStyle.Empty;

		private ConnectDuctStyle roundConn = ConnectDuctStyle.Empty;

		private int shapeNum = -1;
	}
}
